package com.my.test.thread.executors;

import java.util.concurrent.*;

public class MonitorThreadPool {

    private static ExecutorService ees = new ThreadPoolExecutor(5000, 5000, 1000
            , TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(10000));

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 500; i++) {
            ees.execute(() -> {
                //System.out.println("haha");
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        System.out.println("================");
        ees.shutdown();
        ThreadPoolExecutor tpe = ((ThreadPoolExecutor) ees);

        while (true) {
            System.out.println();

            int queueSize = tpe.getQueue().size();
            System.out.println("当前排队线程数：" + queueSize);

            int activeCount = tpe.getActiveCount();
            System.out.println("当前活动线程数：" + activeCount);

            long completedTaskCount = tpe.getCompletedTaskCount();
            System.out.println("执行完成线程数：" + completedTaskCount);

            long taskCount = tpe.getTaskCount();
            System.out.println("总线程数：" + taskCount);

            System.out.println("最大允许线程数：" + tpe.getMaximumPoolSize());
            if (queueSize == 0){
                break;
            }

            Thread.sleep(1000);


        }
    }
}
